const app = getApp();
var url = app.getURL();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    statue: true,
    tabIndex:'1001',
    type:'9902'
  },

  tabFun(e) {
    this.setData({
      tabIndex: e.currentTarget.dataset.index
    })
    let tabIndex = this.data.tabIndex 
    if(tabIndex=='1001'||tabIndex=='1002'){
      this.setData({
        type:'9902'
      })
      this.getList()
    }else if(tabIndex=='1003'){
      this.setData({
        type:'9903'
      })
      this.getList()
    }

  },
  getList(){
    let that = this;
    wx.request({
      url: url+'/order/app/order/auth/getOrderItmeAcc',
      method: "GET",
      header: {
        'content-type': 'application/json',
        token:wx.getStorageSync('cookieToken')
      },
      data:{
        "bussiType": that.data.tabIndex,
      },
      success(res) {
        console.log(res.data)
        that.setData({
          "list": res.data.list.data
        })
      }
    })
  },

  // 身份认证
  indent(e){
    // console.log(e)
    let that = this;
    let id = wx.getStorageSync('message').id;
    // 判断身份状态
    wx.request({
      url: url+'/user/app/user/userAuth/auth/queryAuthStatus',
      method: "POST",
      header: {
        'content-type': 'application/json',
        token:wx.getStorageSync('cookieToken')
      },
      data:{
        "authRole": that.data.type,
        "ownerId": id
      },
      success(res) {
        console.log(res.data)
        if(res.data.data==0){
          wx.showModal({
            title: '温馨提示',
            content:'账号未认证，点击确定认证',
            success(res){
            if(res.confirm){
              that.apply(e)
              }
            }
          })
        }
        if(res.data.data==1){
          that.setData({
            statue: false
          })
          wx.showModal({
            title: '温馨提示',
            content:'账号认证中，请稍后',
          })
        }
        if(res.data.data==2){
          wx.showToast({
            title: '认证通过，跳转中',
            icon: 'loading',
            duration: 600
          });
          that.orderTake(e)
        }
        if(res.data.data==4){
          wx.showModal({
            title: '温馨提示',
            content:'账号认证不通过，请完善资料重新申请'
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 登录认证
    this.isLogin();
    this.getList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  apply(e){
    let id = wx.getStorageSync('message').id;
    wx.request({
      url: url+'/user/app/user/userAuth/auth/submitAuth',
      data:{
        "authRole": this.data.type,
        "ownerId": id
      },
      method:'POST',
      header: {// 设置请求的 header
        'content-type': 'application/json',
        token:wx.getStorageSync('cookieToken')
      },
      success: (res)=>{
        console.log(res)
        wx.showToast({
          title: '已提交申请',
          icon: 'success',
          duration: 1000
        });
        this.setData({
          statue: false
        })
      }
    })
  },
  disApply(){
    // 模拟提交
    wx.showToast({
      title: '提交中',
      icon: 'loading',
      duration: 1000
    });
    this.setData({
      statue: true
    })
    wx.request({
      // url: 'url',
      data:{
        },
      // success: (res)=>{
      //   this.setData({
      //   })
      // }
    })
  },


  //判断是否登录，如果未登录的话就提示登录
  isLogin(){
    let that = this;
  //获取本地缓存的用户信息
 let userName = wx.getStorageSync('cookieNickname')
    if(userName==null||userName==''){
      wx.showToast({
        title: "请先登录",
        icon: "none",
        duration: 2000
        })
        setTimeout(function(){
          wx.switchTab({
            url: '../../my/user/user'
          });
        },600)  
    }
},

// 接单
orderTake(e){
  console.log(e)
    let id=e.currentTarget.dataset.id
    let acceptId = wx.getStorageSync('message').id;
    let tabIndex=e.currentTarget.dataset.type
    wx.request({
      url: url+'/order/app/order/accept/auth/acceptOrder',
      data:{
        "acceptId": acceptId,
        "orderItemId": id
      },
      method:'POST',
      header: {// 设置请求的 header
        'content-type': 'application/json',
        token:wx.getStorageSync('cookieToken')
      },
      success:(res)=>{
        console.log(res)
        wx.showToast({
          title: '接单成功',
          icon: 'success',
          duration: 1000
        }); 
        wx.navigateTo({
          url: '../getorderDetail/getorderDetail?id='+id+'&tabIndex='+tabIndex
        })

      }
    })
}

})